/*
 * ConfigManager.java
 *
 * Created on April 24, 2007, 2:44 PM
 *
 *  Life Logger v1.0 
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.configuration;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * This class is the interface for reading all the configuration parameters. It is
 * implemented with the Singleton pattern and hence has a single instance which
 * is shared across the project.
 * @author anand kishore
 */
public class ConfigManager {
    
    /** The properties */
    private static Properties properties;
    
    /** The config file path */
    private static String configurationFilePath;
    
    /** Write to db */
    private static boolean writeToDb;
    
    /** Write to file */
    private static boolean writeToFile;
    
    /** Parse and store content */
    private static boolean fetchContent;
    
    /** The Singleton object for ConfigManager */
    private static ConfigManager conMgr = null;
    
    /**
     * The singleton constructor
     * @return the singleton instance of the ConfigManager object
     */
    public static ConfigManager getInstance() {
        if(configurationFilePath == "" || configurationFilePath == null) {
            return null;
        }
        
        if(conMgr == null) {
            conMgr = new ConfigManager(configurationFilePath);
        }
        
        return conMgr;
    }
    
    
    /**
     * The private constructor
     * @param path the path of the configuration file
     */
    private ConfigManager(String path) {
        init(new File(path));
    }
    
    /**
     * Initialize the config params
     * @param configFile the File object representing the configuration file
     */
    private void init(File configFile) {
        properties = new Properties();
        try {
            properties.load(new FileInputStream(configFile));
        } catch(Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Could not load config file", e);
        }
    }

    /**
     * Set the configuration file path for the singleton instance
     * @param aConfigurationFilePath the path of the configuration file
     */
    public static void setConfigurationFilePath(String aConfigurationFilePath) {
        configurationFilePath = aConfigurationFilePath;
        getInstance();
    }
    
    /**
     * Get the value for the property specified by the key.
     * @param key the name of the key
     * @return the String value of the key
     */
    public String getValueForKey(String key) {
        return properties.getProperty(key);
    }

    /**
     * Function to check if the data needs to be purged to db
     * @return true if the 'write to db' property is set
     * false otherwise
     */
    public static boolean isWriteToDb() {
        return writeToDb;
    }

    /**
     * Set the write to db property
     * @param aWriteToDb true to turn on the 'write to db' property
     * false otherwise
     */
    public static void setWriteToDb(boolean aWriteToDb) {
        writeToDb = aWriteToDb;
    }

    /**
     * Function to check if the contents need to be fetched and parsed
     * @return true if the 'fetch content' property is set
     * false otherwise
     */
    public static boolean isFetchContent() {
        return fetchContent;
    }

    /**
     * Set the fetc content property
     * @param aFetchContent true to turn on the 'fetch' property
     * false otherwise
     */
    public static void setFetchContent(boolean aFetchContent) {
        fetchContent = aFetchContent;
    }
    
    /**
     * Function to check if the proxy is enabled
     * @return true if the proxy is enabled
     * false otherwise
     */
    public static boolean isProxy() {
        String isProxyValue = conMgr.getValueForKey(Constants.KEY_FOR_IS_PROXY);
        
        boolean isProxy = Boolean.valueOf(isProxyValue);
        
        return isProxy;
    }

    public static String[] getSearchDecorators() {
        String decorators = getInstance().getValueForKey(Constants.KEY_FOR_SEARCH_DECORATORS);
        
        if(decorators != null) {
            return decorators.split(",");
        }
        
        return null;
    }

    public static float getTimeDamperKForBrowse() {
        String value = getInstance().getValueForKey(Constants.KEY_FOR_TIME_DAMPER_K_FOR_BROWSE_ENTITY);
        
        try {
            return Float.valueOf(value);
        } catch(Exception e) {
            return Constants.DEFAULT_VALUE_FOR_TIME_DAMPER_K_FOR_BROWSE;
        }
    }
    
    public static float getTimeDamperKForSearch() {
        String value = getInstance().getValueForKey(Constants.KEY_FOR_TIME_DAMPER_K_FOR_SEARCH_ENTITY);
        
        try {
            return Float.valueOf(value);
        } catch(Exception e) {
            return Constants.DEFAULT_VALUE_FOR_TIME_DAMPER_K_FOR_SEARCH;
        }
    }
    
    public static float getTimeDamperIs() {
        String value = getInstance().getValueForKey(Constants.KEY_FOR_TIME_DAMPER_Is);
        
        try {
            return Float.valueOf(value);
        } catch(Exception e) {
            return Constants.DEFAULT_VALUE_FOR_TIME_DAMPER_Is;
        }
    }
    
    public static float getTimeDamperTkForBrowse() {
        String value = getInstance().getValueForKey(Constants.KEY_FOR_TIME_DAMPER_TK_FOR_BROWSE);
        
        try {
            return Float.valueOf(value);
        } catch(Exception e) {
            return Constants.DEFAULT_VALUE_FOR_TIME_DAMPER_TK_FOR_BROWSE;
        }
    }
    
    public static float getTimeDamperTkForSearch() {
        String value = getInstance().getValueForKey(Constants.KEY_FOR_TIME_DAMPER_TK_FOR_SEARCH);
        
        try {
            return Float.valueOf(value);
        } catch(Exception e) {
            return Constants.DEFAULT_VALUE_FOR_TIME_DAMPER_TK_FOR_SEARCH;
        }
    }
}
